package com.yuqing.sentimentapp.workspace.duty;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.base.BaseProgressView;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.duty.entity.DutyInfoResult;
import com.yuqing.sentimentapp.workspace.duty.entity.DutyReportResult;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shenhe on 2020/4/3.
 */
public class DutyFillInActivity extends BaseActivity implements BaseProgressView<BaseBean> {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.et_leader)
    EditText mEtLeader;
    @BindView(R.id.et_duty_leader)
    EditText mEtDutyLeader;
    @BindView(R.id.et_shift_supervisor)
    EditText mEtShiftSupervisor;
    @BindView(R.id.et_on_duty)
    EditText mEtOnDuty;
    @BindView(R.id.et_on_duty2)
    EditText mEtOnDuty2;
    @BindView(R.id.et_duty_phone)
    EditText mEtDutyPhone;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    private boolean editEnable = true;
    /**
     * 点击填报跳转过来的
     */
    private DutyInfoResult.DataBean dutyInfo;
    /**
     * 从已上报列表跳转过来的
     */
    private DutyReportResult.DataBean.ReportsBean reportsInfo;

    public static void goActivity(Activity activity, DutyInfoResult.DataBean dutyInfo) {
        Intent intent = new Intent(activity, DutyFillInActivity.class);
        intent.putExtra("dutyInfo", dutyInfo);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, DutyReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, DutyFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_duty_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        dutyInfo = ((DutyInfoResult.DataBean) intent.getSerializableExtra("dutyInfo"));
        reportsInfo = ((DutyReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));
    }

    @Override
    public void initView() {
        if (dutyInfo != null) {
            mEtLeader.setText(dutyInfo.getLeader());
            mEtDutyLeader.setText(dutyInfo.getMonitor());
            mEtShiftSupervisor.setText(dutyInfo.getDuty1());
            mEtOnDuty.setText(dutyInfo.getDuty2());
            mEtOnDuty2.setText(dutyInfo.getYqduty());
            mEtDutyPhone.setText(dutyInfo.getPhone());
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtLeader.setText(reportsInfo.getLeader());
            mEtDutyLeader.setText(reportsInfo.getMonitor());
            mEtShiftSupervisor.setText(reportsInfo.getDuty1());
            mEtOnDuty.setText(reportsInfo.getDuty2());
            mEtOnDuty2.setText(reportsInfo.getYqduty());
            mEtDutyPhone.setText(reportsInfo.getPhone());

            mEtLeader.setEnabled(false);
            mEtDutyLeader.setEnabled(false);
            mEtShiftSupervisor.setEnabled(false);
            mEtOnDuty.setEnabled(false);
            mEtOnDuty2.setEnabled(false);
            mEtDutyPhone.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }
    }
    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (dutyInfo == null) {
                    if (TextUtils.isEmpty(mEtLeader.getText().toString())) {
                        showShortToast("带班领导不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(mEtDutyLeader.getText().toString())) {
                        showShortToast("值班领导不能为空");
                        return;
                    }
                    dutyInfo = new DutyInfoResult.DataBean();
                    dutyInfo.setLeader(mEtLeader.getText().toString());
                    dutyInfo.setMonitor(mEtDutyLeader.getText().toString());
                    dutyInfo.setDuty1(TextUtils.isEmpty(mEtShiftSupervisor.getText().toString()) ? "" : mEtShiftSupervisor.getText().toString());
                    dutyInfo.setDuty2(TextUtils.isEmpty(mEtOnDuty.getText().toString()) ? "" : mEtOnDuty.getText().toString());
                    dutyInfo.setYqduty(TextUtils.isEmpty(mEtOnDuty2.getText().toString()) ? "" : mEtOnDuty2.getText().toString());
                    dutyInfo.setPhone(TextUtils.isEmpty(mEtDutyPhone.getText().toString()) ? "" : mEtDutyPhone.getText().toString());
                    saveDutyInfo(dutyInfo);
                } else {
                    if (TextUtils.isEmpty(mEtLeader.getText().toString())) {
                        showShortToast("带班领导不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(mEtDutyLeader.getText().toString())) {
                        showShortToast("值班领导不能为空");
                        return;
                    }
                    dutyInfo.setLeader(mEtLeader.getText().toString());
                    dutyInfo.setMonitor(mEtDutyLeader.getText().toString());
                    dutyInfo.setDuty1(TextUtils.isEmpty(mEtShiftSupervisor.getText().toString()) ? "" : mEtShiftSupervisor.getText().toString());
                    dutyInfo.setDuty2(TextUtils.isEmpty(mEtOnDuty.getText().toString()) ? "" : mEtOnDuty.getText().toString());
                    dutyInfo.setYqduty(TextUtils.isEmpty(mEtOnDuty2.getText().toString()) ? "" : mEtOnDuty2.getText().toString());
                    dutyInfo.setPhone(TextUtils.isEmpty(mEtDutyPhone.getText().toString()) ? "" : mEtDutyPhone.getText().toString());
                    updateDutyInfo(dutyInfo);
                }
                break;
        }
    }

    private void updateDutyInfo(DutyInfoResult.DataBean dutyInfo) {
        ModuleManager.getInstance().updateDutyReportInfoResult(dutyInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveDutyInfo(DutyInfoResult.DataBean dutyInfo) {
        ModuleManager.getInstance().saveDutyReportInfoResult(dutyInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    @Override
    public void onSuccess(BaseBean data) {

    }

    @Override
    public void onSuccess(String message) {
        showShortToast("保存成功");
        finish();
    }

    @Override
    public void onFailure(String message) {
        showShortToast(message);
    }

    @Override
    public void showProgress() {
        startProgressDialog();
    }

    @Override
    public void hideProgress() {
        stopProgressDialog();
    }
}
